package session.stateless;

import entity.Customer;
import entity.CustomerExecutive;
import entity.PurchaseOrder;
import entity.SaleLead;
import entity.SaleLead.CustomerType;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class CustomerSessionBean implements CustomerSessionBeanLocal {
    @PersistenceContext()
    EntityManager em;
    
    public Long createCustomerFromSaleLead(SaleLead saleLead) {
        Customer customer = new Customer();
        
        customer.setSaleLeadName(saleLead.getSaleLeadName());
        customer.setSaleLeadNRIC(saleLead.getSaleLeadNRIC());
        customer.setSaleLeadEmail(saleLead.getSaleLeadEmail());
        customer.setSaleLeadAddress(saleLead.getSaleLeadAddress());
        customer.setSaleLeadContactNumber(saleLead.getSaleLeadContactNumber());
        customer.setCustomerType(saleLead.getCustomerType());

        em.persist(customer);
        return customer.getSaleLeadId();
    }

    public Long createNewCustomer(String name, String NRIC, String email, String address, String contactNumber, CustomerType customerType) {
        Customer customer = new Customer();
        
        customer.setSaleLeadName(name);
        customer.setSaleLeadNRIC(NRIC);
        customer.setSaleLeadEmail(email);
        customer.setSaleLeadAddress(address);
        customer.setSaleLeadContactNumber(contactNumber);
        customer.setCustomerType(customerType);
                
        CustomerExecutive customerExecutive = new CustomerExecutive();
        customer.setCustomerExecutive(customerExecutive);
        
        List<PurchaseOrder> purchaseOrders = new ArrayList<PurchaseOrder>();
        customer.setPurchaseOrders(purchaseOrders);
        
        em.persist(customer);
        return customer.getSaleLeadId();
    }
    
    public void updateCustomer(long customerId, String customerName, String customerNRIC, String customerEmail, String customerAddress, String customerContactNumber, CustomerType customerType, String password, List<PurchaseOrder> purchaseOrders) {
        Customer customer =em.find(Customer.class, customerId);
        
        if (customer ==null)
            System.out.println("Customer does not exist.");
        else
        {
            customer.setSaleLeadName(customerName);
            customer.setSaleLeadNRIC(customerNRIC);
            customer.setSaleLeadEmail(customerEmail);
            customer.setSaleLeadAddress(customerAddress);
            customer.setSaleLeadContactNumber(customerContactNumber);
            customer.setCustomerType(customerType);
            customer.setCustomerPassword(password);
            
            customer.setPurchaseOrders(purchaseOrders);
            //We need to allocate a Customer Executive (randomly) for this customer
            em.flush();                        
        }
    }

   public void deleteCustomer(long customerId) {
        Customer customer = em.find(Customer.class, customerId);
        
        if (customer ==null)
            System.out.println("Customer does not exist.");
        else
        {
            em.remove(customer);
        }
    }

    public List<Customer> getCustomers() {
        Query q=em.createQuery("SELECT c FROM Customer c");
        return q.getResultList();
    }

    public Customer findCustomerByUsername(String username) {
        Query q=em.createQuery("SELECT c FROM Customer c WHERE c.username = :username");
        q.setParameter("username", username);
        
        return (Customer)q.getSingleResult();
    }
}